<?php
// +----------------------------------------------------------------------
// | @filename PermissionLogic.php
// +----------------------------------------------------------------------
// | @author 落花轻舞问君愁 <mwcx1990@qq.com>
// +----------------------------------------------------------------------
// | @datetime 2021-10-29 18:27:16
// +----------------------------------------------------------------------
// | @Description
// +----------------------------------------------------------------------

namespace Addons\Backend\Logics;


use Addons\Backend\Repositories\PermissionRepository;
use Addons\Backend\Transformers\PermissionTransformer;

class PermissionLogic extends BaseLogic
{
    protected $permissionRepository;
    protected $permissionTransformer;

    public function __construct(PermissionRepository $permissionRepository, PermissionTransformer $permissionTransformer)
    {
        $this->permissionRepository = $permissionRepository;
        $this->permissionTransformer = $permissionTransformer;
    }

    /**
     * 获取权限列表
     * @param $filters
     * @return array
     */
    public function list($filters): array
    {
        $roles = $this->permissionRepository->queryList($filters);
        return $this->success(fractal($roles, $this->permissionTransformer)->toArray());
    }

    /**
     * 所有权限
     * @return array
     */
    public function all(): array
    {
        $permissions = $this->permissionRepository->all();
        return $this->success(fractal($permissions, $this->permissionTransformer)->toArray());
    }

    /**
     * 获取角色权限
     * @param $roleId
     * @return array
     */
    public function rolePermissions($roleId): array
    {
        $assignedPermissions = fractal($this->permissionRepository->assignedPermissions($roleId), $this->permissionTransformer)->toArray()['list'];
        $allPermissions = fractal($this->permissionRepository->all(), $this->permissionTransformer)->toArray()['list'];
        $assignedPermissionIds = array_column($assignedPermissions, 'id');

        return $this->success(['allPermissions' => $allPermissions, 'assignedPermissions' => $assignedPermissionIds]);
    }

    /**
     * 创建权限
     * @param $data
     * @return array
     */
    public function create($data): array
    {
        $permission = $this->permissionRepository->create($data);
        return $this->success(fractal($permission, $this->permissionTransformer)->toArray(), '创建权限操作成功');
    }

    /**
     * 更新权限
     * @param $data
     * @return array
     */
    public function update($data): array
    {
        $permission = $this->permissionRepository->update($data);
        return $this->success(fractal($permission, $this->permissionTransformer)->toArray(), '更新权限操作成功');
    }

    /**
     * 删除权限
     * @param $id
     * @return array
     */
    public function delete($id): array
    {
        $permission = $this->permissionRepository->findById($id);

        if (empty($permission)) {
            return $this->failed('权限不存在');
        }

        return $this->permissionRepository->delete($id) ? $this->success([], '删除权限操作成功') : $this->failed([], '删除权限操作失败');
    }
}
